package com.example.term;



import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.qweather.sdk.bean.base.Lang;
import com.qweather.sdk.bean.base.Unit;
import com.qweather.sdk.bean.geo.GeoBean;
import com.qweather.sdk.bean.weather.WeatherDailyBean;
import com.qweather.sdk.bean.weather.WeatherHourlyBean;
import com.qweather.sdk.bean.weather.WeatherNowBean;
import com.qweather.sdk.view.HeConfig;
import com.qweather.sdk.view.QWeather;


public class weather extends Fragment {

    public weather() {
        // Required empty public constructor
    }
    TextView txt_temp,txt_weather,txt_windy,near_day;
    Button btn_qure;
    EditText txt_city;
    String city;
    String city_id;
    String now_weather;
    String now_temp;
    String now_windy;
    String linear_day;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        HeConfig.init("HE2204202252581634", "2366a559f89c449dafb596cf7bd96477"); //初始化
        HeConfig.switchToDevService();  //切换至开发版服务
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_weather, container, false);
    }
    public void onActivityCreated(@Nullable Bundle   savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        btn_qure = (Button)   getActivity().findViewById(R.id.btn_qure);
        txt_temp = (TextView) getActivity().findViewById(R.id.txt_temp);
        txt_weather = (TextView) getActivity().findViewById(R.id.txt_weather);
        txt_city = (EditText) getActivity().findViewById(R.id.txt_city);
        txt_windy = (TextView) getActivity().findViewById(R.id.speed_wind);
        near_day = (TextView) getActivity().findViewById(R.id.liear_weather);
        btn_qure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                city = txt_city.getText().toString().trim(); //获取输入的城市

                QWeather.getGeoCityLookup(getActivity(),city, new QWeather.OnResultGeoListener() {
                    @Override
                    public void onError(Throwable throwable) {
                        Toast.makeText(getActivity(), "第一个失败",Toast.LENGTH_SHORT).show();
//                        txt_temp.setText("");

                    }

                    @Override
                    public void onSuccess(GeoBean geoBean) {
                        city_id = geoBean.getLocationBean().get(0).getId();
                    }
                });


                QWeather.getWeatherNow(getActivity(), city_id, Lang.ZH_HANS, Unit.METRIC, new QWeather.OnResultWeatherNowListener() {
                    @Override
                    public void onError(Throwable e) {
//                        Toast.makeText(getActivity(), "第二个失败",Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onSuccess(WeatherNowBean weatherBean) {
                        now_temp = weatherBean.getNow().getTemp();
                        now_weather = weatherBean.getNow().getText();
                        now_windy = weatherBean.getNow().getWindSpeed().trim();
                        txt_temp.setText(now_temp);
                        txt_weather.setText(now_weather);
                        txt_windy.setText(now_windy);
                    }


                });
                QWeather.getWeather3D(getActivity(), city_id, new QWeather.OnResultWeatherDailyListener() {
                    @Override
                    public void onError(Throwable throwable) {
                        System.out.println(throwable);
                    }

                    @Override
                    public void onSuccess(WeatherDailyBean weatherDailyBean) {
                        linear_day = weatherDailyBean.getDaily().get(0).getTextDay();
                        System.out.println("bbbbbbbbbbbbbb");
                        System.out.println(linear_day);
                    }
                });

            }
        });
}}